import 'package:demo/extension.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_baidu_mapapi_base/flutter_baidu_mapapi_base.dart';
import 'package:flutter_baidu_mapapi_map/flutter_baidu_mapapi_map.dart';

import '../../models/fcl_share_item.dart';
import '../../widgets/fls_common_widget.dart';


class MapPage extends StatefulWidget {
  const MapPage({super.key, required this.title});
  final String title;

  @override
  MapPageState createState() => MapPageState();
}

class MapPageState extends State<MapPage> {
  late BMFMapController myMapController;
  late Size screenSize;

  @override
  void initState() {
    super.initState();
    _buildForInitMap();
  }

    /// 初始化地图
  void _buildForInitMap() {
    WidgetsFlutterBinding.ensureInitialized();
    BMFMapSDK.setAgreePrivacy(true);
    // ak 设置
    BMFMapSDK.setApiKeyAndCoordType(
        'b543YGG0ZCBjTTXtdGhk6O0xiJORJWeP', BMF_COORD_TYPE.GPS);
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        child: Stack(
          children: [
            generateMap(),
            InkWell(
              onTap: () {
                // setState(() {
                //   myMapController.getTrafficEnabled();
                // });
              },
              child: Container(
                width: 100,
                height: 100,
                color: Colors.blue,
              ),
            )
          ],
        ),
      ),
    );
  }
  /// 创建地图 https://lbs.baidu.com/faq/api?title=flutter/loc/create-map/showmap
  Widget generateMap() {
    screenSize = MediaQuery.of(context).size;
    return Container(
      height: screenSize.height,
      width: screenSize.width,
      color: Colors.red,
      child: BMFMapWidget(
        onBMFMapCreated: (mapController) {
          onBMFMapCreated(mapController);
        },
        mapOptions: initMapOptions(),
      ),
    );
  }

   /// 设置地图参数
  BMFMapOptions initMapOptions() {
    BMFMapOptions mapOptions = BMFMapOptions(
      center: BMFCoordinate(39.928637, 116.31849),
      rotateEnabled:false,
      zoomLevel: 15,
      mapPadding: BMFEdgeInsets(top: 0, left: 0, right: 0, bottom: 0),
    );
    return mapOptions;
  }

  /// 创建完成回调
  void onBMFMapCreated(BMFMapController controller) {
    myMapController = controller;
    /// 地图加载回调
    myMapController.setMapDidLoadCallback(callback: () async {
      print('fcl---->mapDidLoad-地图加载完成');
      // 添加标注
      FclShareItem item = FclShareItem(
          userId: '1',
          slbsId: '1',
          latitude: 39.928637,
          longitude: 116.31849,
          avatarUrl: 'https://www.baidu.com/img/bd_logo1.png');
     BMFMarker mark = await addMarker(item);


    BMFMarker marker10 = BMFMarker.icon(
          position: BMFCoordinate(39.928617, 116.30329),
          identifier: 'flutter_marker',
          icon: 'map/chat_location_map_avatar_icon_background_me'.img,
          enabled: true,
          draggable: false,
          centerOffset: BMFPoint(0, -50),
          isOpenCollisionDetectionWithMapPOI: true,
          isOpenCollisionDetectionWithPaoPaoView: true);
      BMFMarker marker11 = BMFMarker.icon(
          position: BMFCoordinate(39.928617, 116.30329),
          identifier: 'flutter_marker',
          icon: 'map/chat_location_map_avatar_icon_sign_me'.img,
          enabled: true,
          draggable: false,
          isOpenCollisionDetectionWithMapPOI: true,
          isOpenCollisionDetectionWithPaoPaoView: true);
      BMFMarker marker12 = BMFMarker.icon(
          position: BMFCoordinate(39.928617, 116.30329),
          identifier: 'flutter_marker',
          icon: 'map/chat_def_avatar_icon'.img,
          enabled: true,
          draggable: false,
          centerOffset: BMFPoint(0, -28),
          isOpenCollisionDetectionWithMapPOI: true,
          isOpenCollisionDetectionWithPaoPaoView: true);
      Future.delayed(const Duration(seconds: 1), () {
        myMapController.addMarkers([mark, marker10, marker11, marker12]);
      });
      /// 地图marker点击回调
      myMapController.setMapClickedMarkerCallback(callback: (BMFMarker marker) {
        print(
            'fcl---->mapClickedMarker-地图marker点击回调 marker: ${marker.identifier}');
      });

      // /// 设定地图中心点坐标
      // myMapController.setCenterCoordinate(BMFCoordinate(29.928617, 116.30329),true);
    });
  }

  Future<BMFMarker> addMarker(FclShareItem item) async {
    // 将Widget转换为图像
    Size size = const Size(180, 280);
    Uint8List imageBytes = await FlsCommonWidget.widgetToImage(
        FlsPaoPaoMarkerWidget(
          item: item,
          size: size,
        ),
        size: size);
    BMFMarker bMarker = BMFMarker.iconData(
      position: BMFCoordinate(item.latitude, item.longitude),
      canShowCallout: false,
      identifier: item.userId,
      iconData: imageBytes,
      enabled: true,
      draggable: false,
      // centerOffset: BMFPoint(0, -50),
      isOpenCollisionDetectionWithMapPOI: true,
      isOpenCollisionDetectionWithPaoPaoView: true, // 设置marker偏移量可以作为弹窗
    );
    return bMarker;
  }
}

